package com.sunwayworld.basemodule.basedata.reporttemplate.service.impl;

import com.sunwayworld.basemodule.basedata.reporttemplate.service.LimsWordTemplateService;
import com.sunwayworld.basemodule.common.utils.AsposeWordUtils;
import com.sunwayworld.cloud.lims.reporttemplate.bean.LimsReportTemplateBean;
import com.sunwayworld.cloud.lims.reporttemplate.service.impl.LimsReportTemplateServiceImpl;
import com.sunwayworld.cloud.lims.reporttemplate.support.word.LimsWordReportGenerator;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.io.file.FilePathDTO;
import com.sunwayworld.framework.io.file.FilePathManager;
import com.sunwayworld.framework.io.file.FileScope;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.DateTimeUtils;
import com.sunwayworld.framework.utils.FileUtils;
import com.sunwayworld.module.item.file.manager.CoreFileManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.Path;
import java.time.LocalDate;

@Repository
@GikamBean
public class LimsWordTemplateServiceImpl extends LimsReportTemplateServiceImpl implements LimsWordTemplateService {

    @Autowired
    private CoreFileManager coreFileManager;

    @Override
    public String samplePrintPdf(RestJsonWrapperBean wrapperBean, Long templateId) {
        LimsReportTemplateBean limsWordTemplateBean = selectFirstByFilter(SearchFilter.instance()
                .match("id", templateId).filter(MatchPattern.EQ));
        if(limsWordTemplateBean == null){
            throw new InvalidDataException("Please configure the barcode prining template");
        }
        MapperParameter parameter = wrapperBean.extractMapFilter();
        LimsWordReportGenerator wordWordGenerator = new LimsWordReportGenerator(limsWordTemplateBean, parameter);
        FilePathDTO templateFile = wordWordGenerator.buildReport();

        String templateFileName = limsWordTemplateBean.getTemplateFileName().split("\\.")[0];
        FilePathDTO pdfFile = FilePathDTO.of(FileScope.temp.name(), templateFile.getTimestamp(), templateFile.getIrregularName()
                , DateTimeUtils.formatLocalDate(LocalDate.now())  + "-" + templateFileName + ".pdf");

        Path localPath = FilePathManager.getLocalPath(templateFile);
        AsposeWordUtils.doc2pdf(localPath.toString(), FilePathManager.getLocalPath(pdfFile).toString());
        FileUtils.deleteQuietly(localPath.toFile());
        // 保存文件
        coreFileManager.upload(pdfFile, FilePathManager.getLocalPath(pdfFile).toFile()); // 上传文件
        return FilePathManager.getUrl(pdfFile);
    }

    @Override
    @Transactional
    public String generatorPDFReport(Long id, RestJsonWrapperBean wrapperBean) {
        MapperParameter parameter = wrapperBean.extractMapFilter();
        LimsReportTemplateBean limsWordTemplateBean = selectById(id);
        LimsWordReportGenerator wordWordGenerator = new LimsWordReportGenerator(limsWordTemplateBean, parameter);
        FilePathDTO templateFile = wordWordGenerator.buildReport();

        String templateFileName = limsWordTemplateBean.getTemplateFileName().split("\\.")[0];
        FilePathDTO pdfFile = FilePathDTO.of(FileScope.temp.name(), templateFile.getTimestamp(), templateFile.getIrregularName()
                , DateTimeUtils.formatLocalDate(LocalDate.now())  + "-" + templateFileName + ".pdf");

        Path localPath = FilePathManager.getLocalPath(templateFile);
        AsposeWordUtils.doc2pdf(localPath.toString(), FilePathManager.getLocalPath(pdfFile).toString());
        FileUtils.deleteQuietly(localPath.toFile());
        // 保存文件
        coreFileManager.upload(pdfFile, FilePathManager.getLocalPath(pdfFile).toFile()); // 上传文件
        return FilePathManager.getUrl(pdfFile);
    }
}
